/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  LogBodyHelper.java                                      
 *  File Contents Outline :  日志体持久化类
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package storage.dbhelper;

import cn.edu.thu.log.web.service.PropertiesReaderServices;
import cn.edu.thu.log.web.service.impl.PropertiesReaderServicesImpl;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.KeySlice;
import storage.util.StorageManager;
import storage.dbobject.DbLogBody;

public class LogBodyHelper {

    /**
     * 将logBody保存到LaUDMS数据库中
     *
     * @param logBody 要存储的模型对象
     */
    public void saveLogBody(DbLogBody logBody) {
        //StorageManager sm = new StorageManager(StorageParameter.db_keySpace, StorageParameter.db_columnFamily_logBody);
                  PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logBody"));
//        sm.open();
        sm.setRowKey(logBody.getRowKey());
        sm.put(DbLogBody.cl_name, logBody.getName());
        sm.put(DbLogBody.cl_abbreviation, logBody.getAbbreviation());
        sm.put(DbLogBody.cl_chinese, logBody.getChinese());
          sm.put(DbLogBody.cl_hasValue, logBody.getHasValue());
        Hashtable<String, String> valueDescriptionMap = logBody.getValueDescriptionMap();
        Enumeration valueEnume = valueDescriptionMap.keys();
        while (valueEnume.hasMoreElements()) {
            String valueTemp = (String) valueEnume.nextElement();
            sm.put(DbLogBody.cl_value + "_" + valueTemp, valueDescriptionMap.get(valueTemp));
        }
//        sm.close();
    }
    /**
     * 将logBody保存到LaUDMS数据库中
     *
     * @param logBody 要存储的模型对象
     */
    public void updateLogBody(DbLogBody logBody) {
                  PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logBody"));
//        sm.open();
        sm.delete(logBody.getRowKey());
        sm.setRowKey(logBody.getRowKey());
        sm.put(DbLogBody.cl_name, logBody.getName());
        sm.put(DbLogBody.cl_abbreviation, logBody.getAbbreviation());
        sm.put(DbLogBody.cl_chinese, logBody.getChinese());
         sm.put(DbLogBody.cl_hasValue, logBody.getHasValue());
        Hashtable<String, String> valueDescriptionMap = logBody.getValueDescriptionMap();
        Enumeration valueEnume = valueDescriptionMap.keys();
        while (valueEnume.hasMoreElements()) {
            String valueTemp = (String) valueEnume.nextElement();
            sm.put(DbLogBody.cl_value + "_" + valueTemp, valueDescriptionMap.get(valueTemp));
        }
//        sm.close();
    }
    /**
     * 将一系列xes保存到LaUDMS数据库中
     *
     * @param xeses 要存储的模型对象列表
     */
    public void saveLogBodys(List<DbLogBody> logBodys) {
                  PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logBody"));
//        sm.open();
        for (int i = 0; i < logBodys.size(); i++) {
            DbLogBody logBody = logBodys.get(i);
            sm.setRowKey(logBody.getRowKey());
            sm.put(DbLogBody.cl_name, logBody.getName());
            sm.put(DbLogBody.cl_abbreviation, logBody.getAbbreviation());
            sm.put(DbLogBody.cl_chinese, logBody.getChinese());
              sm.put(DbLogBody.cl_hasValue, logBody.getHasValue());
            Hashtable<String, String> valueDescriptionMap = logBody.getValueDescriptionMap();
            Enumeration valueEnume = valueDescriptionMap.keys();
            while (valueEnume.hasMoreElements()) {
                String valueTemp = (String) valueEnume.nextElement();
                sm.put(DbLogBody.cl_value + "_" + valueTemp, valueDescriptionMap.get(valueTemp));
            }

        }
//        sm.close();
    }

    public List<DbLogBody> getLogBodys(final String columnName, final String value) {
        List<DbLogBody> result = new LinkedList<DbLogBody>();
                     PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logBody"));
//        sm.open();
        List<KeySlice> crs = sm.get(columnName, value);
        try {
            for (KeySlice ks : crs) {
                String rowKey = new String(ks.getKey());
                String name = null, abbreviation = null, chinese = null, hasValue = null;
                Hashtable<String, String> valueDescriptionMap = new Hashtable<String, String>();
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());

                    if (columnsName.equals(DbLogBody.cl_name)) {
                        name = columnsValue;
                    } else if (columnsName.equals(DbLogBody.cl_abbreviation)) {
                        abbreviation = columnsValue;
                    } else if (columnsName.equals(DbLogBody.cl_chinese)) {
                        chinese = columnsValue;
                    } else if (columnsName.equals(DbLogBody.cl_hasValue)) {
                        hasValue = columnsValue;
                    } else if (columnsName.startsWith(DbLogBody.cl_value)) {
                        String valueTemp = columnsName.substring(DbLogBody.cl_value.length() + "_".length());
                        valueDescriptionMap.put(valueTemp, columnsValue);
                    }
                }
                DbLogBody pLogBody = new DbLogBody(rowKey, name, abbreviation, chinese,hasValue,valueDescriptionMap);
                result.add(pLogBody);
            }
        } catch (Exception ex) {
            Logger.getLogger(DbLogBody.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return result;
    }

    public void deleteLogBody(final String rowKey) {
                     PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logBody"));
//        sm.open();
        sm.delete(rowKey);
//        sm.close();
    }

    public ArrayList<String> getAllLogBodyName() {
                  PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logBody"));
//        sm.open();
        List<KeySlice> crs = sm.getAll();
        ArrayList<String> logBodyNameList = new ArrayList<String>();
        try {
            for (KeySlice ks : crs) {
                String name = null;
                for (ColumnOrSuperColumn column : ks.getColumns()) {
                    String columnsName = new String(column.getColumn().getName());
                    String columnsValue = new String(column.getColumn().getValue());
                    if (columnsName.equals(DbLogBody.cl_name)) {
                        name = columnsValue;
                    }
                }
                if (!logBodyNameList.contains(name)) {
                    logBodyNameList.add(name);
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(LogBodyHelper.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
//            sm.close();
        }
        return logBodyNameList;
    }

    public void addValueDesceiption(DbLogBody logBody, String value, String description) {
        List<DbLogBody> result = new LinkedList<DbLogBody>();
                       PropertiesReaderServices x1 = PropertiesReaderServicesImpl.instance();
        StorageManager sm = new StorageManager(x1.getProperties("db_keySpace"), x1.getProperties("db_columnFamily_logBody"));
//        sm.open();
        sm.setRowKey(logBody.getRowKey());
        if (!logBody.getHasValue().equalsIgnoreCase("yes")) {
            sm.put(DbLogBody.cl_hasValue, "yes");
            logBody.setHasValue("yes");
        }
        sm.put(DbLogBody.cl_value + "_" + value, description);
        //   sm.put(DbLogBody.cl_description + "_" + value, description);
//        sm.close();
    }
    public static void main(String [] args){
        LogBodyHelper lbh = new LogBodyHelper();
        List<DbLogBody> lbn = lbh.getLogBodys(DbLogBody.cl_name, "PageType");
    }
}
